﻿using System;
using System.Collections.Generic;

namespace MAF.Common
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class MemRepo<T> : IRepository<T> where T : IID<Guid>, new()
    {
        /// <summary>
        /// 存储新实体
        /// </summary>
        /// <param name="info"></param>
        public void Add(T info)
        {
            if (null == info)
                throw new ArgumentNullException();

            _repo.Add(info);
        }

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="info"></param>
        public int Update(T info)
        {
            if (null == info)
                throw new ArgumentNullException();

            int index = _repo.FindIndex(delegate(T entity)
            {
                if (entity.Id == info.Id)
                    return true;
                else
                    return false;
            });

            if (-1 == index)
                return 0;

            _repo[index] = info;
            return 1;
        }

        /// <summary>
        /// 批量更新实体
        /// </summary>
        /// <param name="q"></param>
        /// <returns></returns>
        public int UpdateByQuery(Query<T> q)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 移除实体
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int Remove(Guid id)
        {
            var q = new Query<T>();
            q.AddCriterion("Id", Op.Equals, id);
            return RemoveByQuery(q);
        }

        /// <summary>
        /// 批量移除实体
        /// </summary>
        /// <param name="q"></param>
        /// <returns></returns>
        public int RemoveByQuery(Query<T> q)
        {
            return _repo.RemoveAll(delegate(T entity)
            {
                foreach (Criterion<T> c in q)
                {
                    return c.IsMath(entity);
                }
                return false;
            });
        }

        /// <summary>
        /// 查询实体明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetById(Guid id)
        {
            Query<T> q = new Query<T>();
            q.AddCriterion("Id", Op.Equals, id);
            var list = GetByQuery(q);
            if (0 == list.Count)
                return default(T);
            else
                return list[0];
        }

        /// <summary>
        /// 查询实体清单
        /// </summary>
        /// <param name="vo"></param>
        /// <returns></returns>
        public List<T> GetByQuery(Query<T> q)
        {
            if (null == q)
                return _repo;

            return _repo.FindAll(delegate(T entity)
            {
                foreach (Criterion<T> c in q)
                {
                    return c.IsMath(entity);
                }
                return false;
            });
        }

        /// <summary>
        /// 查询实体清单的数量
        /// </summary>
        /// <param name="vo"></param>
        /// <returns></returns>
        public int CountByQuery(Query<T> q)
        {
            return GetByQuery(q).Count;
        }

        /// <summary>
        /// 用户内存存储器
        /// </summary>
        private List<T> _repo = new List<T>();


        public int Update(List<T> entities)
        {
            throw new NotImplementedException();
        }

        public int Remove(T entity)
        {
            throw new NotImplementedException();
        }

        public int Remove(string ids)
        {
            throw new NotImplementedException();
        }

        public int Remove(List<T> entities)
        {
            throw new NotImplementedException();
        }
    }
}
